<?
namespace Api
{
 /**
  * Operation on files
  */
  class File
  {
        /**
         * ls files from  directory with $ext extension.
         *
         * @param string $dir - directory
         * @param string $ext - extension
         * @param bool $onlynames - only file name, without ext.
         *
         * @return array $files - matched files
         */
        public function ls($dir, $ext, $onlynames=false)
        {
            $files = array();
            $dir = BASE_PATH.'/'.$dir;
            $dh  = opendir($dir);           
            while (false !== ($filename = readdir($dh))) {
              if(is_file($dir.'/'.$filename) && preg_match('/\.'.$ext.'$/',$filename))
                {
                    
                    $files[] = ($onlynames?preg_replace('/\.'.$ext.'$/','',$filename):$filename);
                }
                
            }

            return $files;
        }
        
        /**
         * Delete all files from directory
         */
        public function rm($dir)
        {
            $files = array();
            $dir = BASE_PATH.'/'.$dir;
            $dh  = opendir($dir);           
            while (false !== ($filename = readdir($dh))) {
              if(is_file($dir.'/'.$filename))
                {
                  unlink($dir.'/'.$filename);
                }
                
            }

            return $files;
        }
  }
}
?>